<? 
	$title="User List"; 
	$pageid="4";
	$accessid=",a10,";

	if(isset($_POST['btnsubmit']))
	{
		require("required/dbconnect.php");
		$id=$_POST['hidid'];
		$voucher_code=mysql_real_escape_string($_POST['voucher_code']);
		$current_voucher_code=mysql_real_escape_string($_POST['current_voucher_code']);
		$voucher_name=mysql_real_escape_string($_POST['voucher_name']);
		$minimum_purhcase=mysql_real_escape_string($_POST['minimum_purchase']);
		$discount_amount=mysql_real_escape_string($_POST['discount_amount']);
		$discount_type=mysql_real_escape_string($_POST['discount_type']);
		$minimum_purchase=mysql_real_escape_string($_POST['minimum_purchase']);
		$times_to_use=mysql_real_escape_string($_POST['times_to_use']);
		$email_subject=mysql_real_escape_string($_POST['email_subject']);
		$email_content=mysql_real_escape_string($_POST['email_content']);
		$status=mysql_real_escape_string($_POST['status']);
		$start_date=mysql_real_escape_string($_POST['start_date']);
		$end_date=mysql_real_escape_string($_POST['end_date']);
		
		
		if($current_voucher_code!=$voucher_code)
		{
			$chk=mysql_query("select count(*) as result from os_discount_voucher where voucher_code='$voucher_code'");
			
			if(mysql_result($chk,0,"result")>0)
			{
				header("Location:dv_edit.php?action=2&id=$id");
				die();
			}
		}
		
		$update=mysql_query("update os_discount_voucher set voucher_code='$voucher_code',voucher_name='$voucher_name',minimum_purchase='$minimum_purchase',discount_amount='$discount_amount',discount_type='$discount_type',
		times_to_use='$times_to_use',email_subject='$email_subject',email_content='$email_content',status='$status',start_date='$start_date',end_date='$end_date' where id=$id")or die(mysql_error());
		
		header("Location:dv_edit.php?id=$id&action=1");
	}
	
	require("required/dbconnect.php"); 
	require("required/function.php");
	require("required/header.php");
	
	checkAccess("allow_gift_management",1);
?>
<script src="required/js/jquery-1.4.2.js"></script>
<script src="required/js/common_javascript.js"></script>
<?

		
	if(!empty($_GET['action']))
	{
		switch($_GET['action'])
		{
			case 1:
				$msg="<div class='dw_success'>Data Updated</div>";
				break;
			case 2:
				$msg="<div class='dw_failed'>Voucher code already exist,update failed!</div>";
				break;
			case 3:
				$msg="<div class='dw_success'>Data Updated</div>";
				break;	
			default:
				$msg="";
				break;
		}
		echo "<script>displayMsg('divMsg')</script>";	
	}else{
		$msg="";
	}
	
	
	$id=$_GET['id'];
	
	$get_info=mysql_query("select * from os_discount_voucher where id=$id");
	
	
	
	$a=mysql_fetch_array($get_info,MYSQL_ASSOC);
	
	if($a['discount_type']=="M")
	{
		$type_slt1="selected";
		$type_slt2="";
	}else{
		$type_slt1="";
		$type_slt2="selected";
	}
	
	if($a['status']==1)
	{
		$sslt1="selected";
		$sslt0="";
		$status_msg="<div class='dw_info'>This voucher is <strong>ACTIVATED</strong>, member can use this voucher any time</div>";
	}else{
		$sslt1="";
		$sslt0="selected";
		$status_msg="<div class='dw_info'>This voucher is <strong>DISABLED</strong></div>";
	}
		
?>
<table width="100%"  cellpadding="0" cellspacing="5" height="300">
<tr>
	<td valign="top">
			
            
			<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
            <script type="text/javascript" src="required/js/calendarDateInput.js"></script>
			<table width="100%">
			
				<tr>
					<td colspan="3" class="title">
					Edit Discount Voucher
					</td>
				</tr>
               <Tr>
               		<td colspan="3">
                    <?= $status_msg ?>
                    <div id="divMsg"><?= $msg ?></div>
                    </td>
               </Tr>
                <tr>
                    <td colspan="2" class="sub_title_line" ><br />Settings</td>
                </tr>
                <tr>
					<td>Status</td><td width="82%">
  					<select name="status">
                    	<option <?= $sslt1 ?> value="1">Active</option>
                        <option <?= $sslt0 ?> value="0">Disabled</option>
                    </select>
  					</td>
			  	</tr>
                
                <tr>
               		<Td>Valid From</Td><td><script>var start_date="<?= $a['start_date'] ?>" ;
									DateInput('start_date', true, 'YYYY-MM-DD',start_date)</script></td>
               </tr>
               
               <tr>
               		<Td>Valid To</Td><td><script>var end_date="<?= $a['end_date'] ?>" ;
									DateInput('end_date', true, 'YYYY-MM-DD',end_date)</script></td>
               </tr>		
                
                <Tr>
               	  <td>Voucher Name</td><td width="82%"><input type="text" value="<?= $a['voucher_name'] ?>" name="voucher_name" maxlength="50" size="30"></td>
             	</Tr>
                <Tr>
               	  <td>Voucher Code</td><td width="82%">
                  <input type="text" value="<?= $a['voucher_code'] ?>" name="voucher_code" maxlength="50" size="30">
                  <input type="hidden" value="<?= $a['voucher_code'] ?>" name="current_voucher_code" />
                  </td>
             	</Tr>
                <Tr>
               	  <td>Times To Use</td><td width="82%"><input type="text" value="<?= $a['times_to_use'] ?>" name="times_to_use" id="times_to_use" maxlength="2" size="5" onkeyup="removeAlphabet(this.name)"></td>
             	</Tr>
                <tr>
					<td>Discount Amount</td><td width="82%"><input type="text" value="<?= $a['discount_amount'] ?>" name="discount_amount"  id="discount_amount" maxlength="3" size="5" onkeyup="removeAlphabet(this.name)">
  					<select name="discount_type">
                    	<option <?= $type_slt1 ?> value="M">% Off</option>
                        <option <?= $type_slt2 ?> value="P">$ Off</option>
                    </select>
  
  					</td>
			  </tr>
               <tr>
              	<Td>Minimum Order</Td><Td>RM <input type="text" value="<?= $a['minimum_purchase'] ?>" name="minimum_purchase"  id="minimum_purchase" maxlength="3" size="5" onkeyup="removeAlphabet(this.name)"></Td>
              </tr>
               <tr>
                    <td colspan="2" class="sub_title_line" ><br />Email Content</td>
                </tr>
             
				 <tr valign="top">
                 	<Td>Use the keyword to replace the data in email's subject and content</Td>
                    <td> 
                    <table class="solidborder" width="400">
                        <tr class="table_header">
                            <td>Keyword</td><td>To replace </td>
                        </tr>
                        <tr>
                            <td>[%name%]</td><td>Member's name</td>
                        </tr>
                        <tr>
                           <td>[%voucher_code%]</td><td>Voucher code</td>
                        </tr>
                        <tr>
                           <td>[%birth_date%]</td><td>Member Birth Date</td>
                        </tr>
                         <tr>
                           <td>[%expiry_date%]</td><td>Voucher Expiry Date</td>
                        </tr>
                         <tr>
                           <td>[%minimum_purchase%]</td><td>Minimum Purchase</td>
                        </tr>
                        
                     </table>
                     
                     </td>
                 </tr>       
                
                 <tr>
                   <Td>Email Subject</Td><td><input type="text" value="<?= $a['email_subject'] ?>" name="email_subject" size="50"   /></td>
                 </tr>
                 <tr valign="top">
                    <td>Email Content</td>  
                    <td width="82%" valign="top">
                                <? include_once("fckeditor/fckeditor.php") ?>
                                <?php
                                    $oFCKeditor = new FCKeditor('email_content') ;
                                    $oFCKeditor->ToolbarSet = 'DeswinBasic';
                                    $oFCKeditor->BasePath = './fckeditor/' ;
                                    $oFCKeditor->Value = $a['email_content'] ;
                                    $oFCKeditor->Create() ;
                                ?>             
                    </td>
              </tr>
                
				
			
				<td width="18%">
                	<input type="hidden" value="<?= $id ?>" name="hidid" />
				  <input type="submit" value="Save" class="button" name="btnsubmit">
                  <input type="button" value="Back" class="button" name="btnsubmit" onclick="window.open('dv_management.php','_self')">
				</td>
			</table>
			</form>
		</td>
	</tr>
	
	
</table>
<? require("required/footer.php"); ?>

